<template>
	<view>
		<form style="background: white">
			<view class="cu-form-group margin-top">
				<view class="title">养殖仓库</view>
				<picker @change="PickerChange" :value="index" :range="breedingHousePicker">
					<view class="picker" style="text-align: right; color: #808080; font-size: 30rpx;">
						{{changeIndex>-1?breedingHousePicker[index]:'请选择养殖仓'}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group">
				<view class="title">猪栏编号</view>
				<input maxlength="10" v-model="pigstyNo" placeholder="请输入猪栏编号" name="input" style="text-align: right; margin-right: 20rpx;"></input>
			</view>
			<!-- <view class="cu-form-group">
				<view class="title">设备绑定</view>
				<picker @change="devicePickerChange" :value="deviceIndex" :range="devicePicker">
					<view class="devicePicker" style="text-align: right; color: #808080; font-size: 30rpx;">
						{{deviceChangeIndex>-1?devicePicker[deviceIndex]:'请选择设备'}}
					</view>
				</picker>
			</view> -->
		</form>
	</view>
</template>

<script>
	export default {
		data () {
			return {
				index: 0,
				changeIndex: -1,
				breedingHousePicker: [],//养殖仓picker选择列表
				pigstyNo: '',
				warehouse_id_List: []
				
				//deviceIndex:0,
				//deviceChangeIndex: -1,
				//devicePicker: ['193.1.168.111', '193.1.168.112', '193.1.168.113']
				
			}
		},
		onNavigationBarButtonTap() {
			this.submit()
		},
		methods: {
			PickerChange: function(e) {
				this.index = e.target.value
				this.changeIndex = this.index
				console.log(this.index)
			},
			
			// devicePickerChange: function(e) {
			// 	this.deviceIndex = e.target.value
			// 	this.deviceChangeIndex = this.deviceIndex
			// },
			
			//提交
			submit (index) {
				if(this.changeIndex <= -1){
					uni.showToast({
						title:"请选择养殖仓",
						icon: "none"
					})
					return;
				}
				else if(this.pigstyNo.toString() === ""){
					uni.showToast({
						title:"请填写正确的猪栏编号",
						icon: "none"
					})
					return;
				}else{
					uni.request({
						url:this.confige.webUrl+'/livestock/addPigsty',
						data:{
							farm_id: this.User.userInfo.farm_id,
							farm_name: this.User.userInfo.farm_name,
							warehouse_id: this.warehouse_id_List[this.index],
							warehouse_name: this.breedingHousePicker[this.index],
							pigsty_number: this.pigstyNo
						},
						method: "POST",
						header:{
							"X-Token":this.User.token
						},
						success: (res) => {
							console.log(res)
							if(res.data.code == 200){
								uni.showModal({
									title: "提示",
									showCancel: false,
									content: "登记成功",
									success: (res) => {
										if(res.confirm){
											uni.navigateBack({
												delta: 1
											})
										}
									}
								})	
							}
							else if(res.data.code == 400) {
								uni.showModal({
									title: "提示",
									showCancel: false,
									content: res.data.message
								})
							}
						}
					})
				}
			},
			
			//初始化数据
			init () {
				uni.request({
					url:this.confige.webUrl+'/livestock/selWarehouse',
					data:{
						farm_id: this.User.userInfo.farm_id
					},
					header:{
						"X-Token":this.User.token
					},
					success: (res) => {
						console.log(res)
						//获取并设置养殖仓picker选择列表
						res.data.result.forEach((item,index) => {
							this.breedingHousePicker.push(item.warehouse_serial_number)
							this.warehouse_id_List.push(item.warehouse_id)
						})
						console.log(this.breedingHousePicker)
						console.log(this.warehouse_id_List)
					}
				})
			}
		},
		onLoad() {
			
		},
		onReady() {
			this.init()
		},
		onShow() {
			
		}
	}
</script>

<style lang="less">
	@import "../../../common/colorui/main.css";
	@import "../../../common/colorui/icon.css";
	
	page {
		background: #EEEEEE;
	}
	
	.submit {
		width: 710rpx;
		height: 80rpx;
		position: fixed;
		bottom: 30rpx;
	}
	
	.submit button {
		background-color: #4AC997;
		color: white;
		font-weight: bold;
		border-radius: 60rpx;
		margin-left: 40rpx;
		height: 80rpx;
	}
</style>
